package com.jxsd.edu.cn.tools;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.jxsd.edu.cn.tools.DbTool;

public class ResultList<T> implements ResultHandler<List<T>>{
	
	private String parseMethodName(String columnName){
		
		String temp = "set";
		temp+=columnName.substring(0, 1).toUpperCase();
		temp+=columnName.substring(1);
		return temp;
	}

	@SuppressWarnings("unchecked")
	public List<T> handler(String sql,Object[] params, Class<?> clazz) throws Exception {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<T> lists = new ArrayList<T>();
		try {
			conn = DbTool.getConnection();
			stmt = conn.prepareStatement(sql);
			
			if(params!=null)
			{
				for(int i=0;i<params.length;i++)
				{
					stmt.setObject(i+1, params[i]);
				}
			}
			
			rs = stmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			while(rs.next()){
				T destObject = (T) clazz.newInstance();
				Method method = null;
				for(int i=1;i<=rsmd.getColumnCount();i++){
					method = clazz.getMethod(parseMethodName(rsmd.getColumnLabel(i)),new Class[]{rs.getObject(i).getClass()});
					method.invoke(destObject, new Object[]{rs.getObject(i)});
				}
				lists.add(destObject);
			}
		} finally{
			DbTool.close(stmt, conn, rs);
		}
		return lists;
	}
	
	@SuppressWarnings("unchecked")
	public Page<T> pageHandler(PageBean<T>  pagebean,String sql,Object[] params, Class<?> clazz) throws Exception {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Page<T>  page=pagebean.getPage(sql,params);
		
		String  pageSql=pagebean.getPageSQl(sql);
		try {
			conn = DbTool.getConnection();
			stmt = conn.prepareStatement(pageSql);
			
			if(params!=null)
			{
				for(int i=0;i<params.length;i++)
				{
					stmt.setObject(i+1, params[i]);
				}
			}
			
			rs = stmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			while(rs.next()){
				T destObject = (T) clazz.newInstance();
				Method method = null;
				for(int i=1;i<=rsmd.getColumnCount();i++){
					method = clazz.getMethod(parseMethodName(rsmd.getColumnLabel(i)),new Class[]{rs.getObject(i).getClass()});
					method.invoke(destObject, new Object[]{rs.getObject(i)});
				}
				page.getList().add(destObject);
			}
		} finally{
			DbTool.close(stmt, conn, rs);
		}
		return page;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<T> handlerState(String sql, Class<?> clazz) throws Exception {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		List<T> lists = new ArrayList<T>();
		try {
			conn = DbTool.getConnection();
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(sql);
			ResultSetMetaData rsmd = rs.getMetaData();
			while(rs.next()){
				T destObject = (T) clazz.newInstance();
				Method method = null;
				for(int i=1;i<=rsmd.getColumnCount();i++){
					method = clazz.getMethod(parseMethodName(rsmd.getColumnLabel(i)),new Class[]{rs.getObject(i).getClass()});
					method.invoke(destObject, new Object[]{rs.getObject(i)});
				}
				lists.add(destObject);
			}
		} finally{
			DbTool.close(stmt, conn, rs);
		}
		return lists;
	}
	
	
	@SuppressWarnings("unchecked")
	public Page<T> pageHandlerState(PageBean<T>  pagebean,String sql, Class<?> clazz) throws Exception {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		Page<T>  page=new  Page<T> ();
		page.setList(new  ArrayList<T>());
		page.setCurrent_page(pagebean.getCurrentPage());
		page.setPageSize(pagebean.getPageSize());
		
		String  pageSql=pagebean.getPageSQl(sql);
		try {
			conn = DbTool.getConnection();
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(pageSql);
			ResultSetMetaData rsmd = rs.getMetaData();
			while(rs.next()){
				T destObject = (T) clazz.newInstance();
				Method method = null;
				for(int i=1;i<=rsmd.getColumnCount();i++){
					method = clazz.getMethod(parseMethodName(rsmd.getColumnLabel(i)),new Class[]{rs.getObject(i).getClass()});
					method.invoke(destObject, new Object[]{rs.getObject(i)});
				}
				page.getList().add(destObject);
			}
		} finally{
			DbTool.close(stmt, conn, rs);
		}
		return page;
	}
}